# 练习题：学籍注册程序
# 需求：
# 1. 要求⽤户输⼊姓名、年龄、⼿机号、身份证号、所选课程，然后为学员完成注册
# 2. ⼿机号、身份证号唯⼀
# 3. 可选的课程只能从Python、Linux、⽹络安全、前端、数据分析 这⼏⻔⾥选
# 4. 学员信息存⼊⽂件
import os

# 定义可选课程列表
courses = ["Python", "Linux", "网络安全", "前端", "数据分析"]

# 存储文件路径
file_path = 'students.txt'

# 读取已注册学员信息
students = []
if os.path.exists(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        students = file.readlines()

# 将已注册学员的手机号和身份证号记录下来便于核查唯一性
registered_phones = [line.split(',')[2] for line in students]
registered_ids = [line.split(',')[3] for line in students]

# 输入学员信息
name = input("请输入姓名：")
age = input("请输入年龄：")

while True:
    phone = input("请输入手机号：")
    if phone in registered_phones:
        print("该手机号已注册，请使用另一手机号。")
    else:
        break

while True:
    id_number = input("请输入身份证号：")
    if id_number in registered_ids:
        print("该身份证号已注册，请使用另一身份证号。")
    else:
        break

while True:
    course = input(f"请选择课程 ({'，'.join(courses)}): ")
    if course not in courses:
        print(f"课程选择错误, 可选课程仅限于：{'，'.join(courses)}。")
    else:
        break

# 将学员信息存储到文件
with open(file_path, 'a', encoding='utf-8') as file:
    file.write(f"{name},{age},{phone},{id_number},{course}\n")

print("学员注册成功！")
